<?php

namespace app\admin\controller;

use app\admin\validate\Specs as specsValidate;
use app\admin\business\SpecsValue as specsValueBus;
use app\admin\validate\Specs;
use think\Exception;

class SpecsValue
{
    public function save()
    {
        $specsId = input("param.specs_id", '', 'intval');
        $name = input("param.name");
        $data = [
            'specs_id' => $specsId,
            'name' => $name,
        ];
        $validate = new specsValidate();
        if (!$validate->scene('save')->check($data)) {
            return show(config("status.error"), $validate->getError());
        }
        $specsBus = new specsValueBus();

        $res = $specsBus->getNamesBySpecsId($data);
        if (!$res) {
            return show(config("status.error"), '该规格已存在，请重新输入');
        }

        $id = $specsBus->add($data);
//        dump($id);
//        exit();
        if ($id) {
            return show(config("status.success"), 'OK', ['id' => $id]);
        } else {
            return show(config("status.error"), "添加失败");
        }
    }

    public function getBySpecsId()
    {
        $specsId = input("param.specs_id", '', 'intval');
        $data = [
            'specs_id' => $specsId,
        ];
        $validate = new specsValidate();
        if (!$validate->scene('normal')->check($data)) {
            return show(config("status.error"), $validate->getError());
        }
        $specsBus = new specsValueBus();
        $res = $specsBus->getBySpecsId($data);
        if ($res) {
            return show(config("status.success"), "OK", $res);
        } else {
            return show(config("status.error"), "没有数据");
        }

    }



    public function test()
    {
        $data = [
            'specs_id' => 1,
            'name' => '青色',
        ];
        $specsBus = new specsValueBus();
        $specsBus->add($data);
    }

}